package cn.thens.kdroid.core.storage

internal class MapStorage<K, V>(private val dataMap: MutableMap<K, V> = HashMap()) : Storage<K, V> {

    override fun node(key: K): Storage.Node<V> {
        return Node(dataMap, key)
    }

    override fun clear() {
        dataMap.clear()
    }

    override fun getAll(): Map<K, V> {
        return HashMap(dataMap)
    }

    override fun getKeys(): Set<K> {
        return dataMap.keys
    }

    class Node<K, V>(private val dataMap: MutableMap<K, V>, private val key: K) : Storage.Node<V> {
        override fun get(): V {
            return dataMap[key]!!
        }

        override fun put(value: V) {
            dataMap[key] = value
        }

        override fun clear() {
            dataMap.remove(key)
        }

        override fun exists(): Boolean {
            return dataMap.containsKey(key)
        }
    }
}
